package com.tjetc.service.impl;

import com.tjetc.dao.BedDao;
import com.tjetc.dao.DepartmentDao;
import com.tjetc.dao.RoomDao;
import com.tjetc.dao.impl.BedDaoImpl;
import com.tjetc.dao.impl.DepartmentDaoImpl;
import com.tjetc.dao.impl.RoomDaoImpl;
import com.tjetc.domain.Bed;
import com.tjetc.service.BedService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class BedServiceImpl implements BedService {
    private BedDao bedDao=new BedDaoImpl();
    private DepartmentDao departmentDao=new DepartmentDaoImpl();
    private RoomDao roomDao=new RoomDaoImpl();

    @Override
    public List<Bed> findByRid(int id) {
        try {
            return bedDao.findByRid(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Bed> findByDepid(int depid) {
        try {
            return bedDao.findByDepid(depid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Bed findByUid(int uid) {
        try {
            return bedDao.findByUid(uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(String number) {
        try {
            return bedDao.getCount(number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getCount1(int status, String number) {
        try {
            return bedDao.getCount1(status,number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Bed> list(String number, PageInfo<Bed> pageInfo) {
        try {
            List<Bed> list=bedDao.list(number,pageInfo);
            for (Bed bed : list) {
                bed.setDepartment(departmentDao.getById(bed.getDepid()));
                bed.setRoom(roomDao.findById(bed.getRoomid()));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Bed> list1(int status, String number, PageInfo<Bed> pageInfo) {
        try {
            List<Bed> list=bedDao.list1(status,number,pageInfo);
            for (Bed bed : list) {
                bed.setDepartment(departmentDao.getById(bed.getDepid()));
                bed.setRoom(roomDao.findById(bed.getRoomid()));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getRCount(int roomid) {
        try {
            return bedDao.getRCount(roomid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return roomid;
    }

    @Override
    public Bed findByNumber(String number,int roomid) {
        try {
            return bedDao.findByNumber(number,roomid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Bed bed) {
        try {
            Db.startTransaction();
            bedDao.add(bed);
            departmentDao.setbed(bed.getDepid());
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        }finally {
            //关闭连接
            Db.close();
        }
        return false;
    }

    @Override
    public boolean update(Bed bed) {
        try {
            return bedDao.update(bed)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean del(Bed bed) {
        try {
            Db.startTransaction();
            bedDao.del(bed.getId());
            departmentDao.setbed1(bed.getDepid());
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        }finally {
            Db.close();
        }
        return false;
    }

    @Override
    public Bed findById(int id) {
        try {
            return bedDao.findById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updatePrice(Bed bed) {
        try {
            return bedDao.updatePrice(bed)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


}
